#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "renderer.h"
#include "scene3d.h"
#include "QMessageBox"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->actionView->setChecked(true);
    connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(set_text_lambda(int)));
    connect(ui->horizontalSlider_2, SIGNAL(valueChanged(int)), this, SLOT(set_text_tropism(int)));
    connect(ui->horizontalSlider_3, SIGNAL(valueChanged(int)), this, SLOT(set_text_aux_angle(int)));
    connect(ui->horizontalSlider_4, SIGNAL(valueChanged(int)), this, SLOT(set_text_shed_thres(int)));
    connect(ui->horizontalSlider_5, SIGNAL(valueChanged(int)), this, SLOT(set_text_term_weight(int)));
    connect(ui->horizontalSlider_6, SIGNAL(valueChanged(int)), this, SLOT(set_text_aux_weight(int)));
    connect(ui->shadowProp, SIGNAL(clicked(bool)), this, SLOT(shadowSelected(bool)));
    connect(ui->spaceCol, SIGNAL(clicked(bool)), this, SLOT(shadowDeselected(bool)));
    connect(ui->drawAlgo, SIGNAL(stateChanged(int)), this, SLOT(drawAlgo(int)));
    connect(ui->metamer, SIGNAL(stateChanged(int)), this, SLOT(addMultipleMetamers(int)));
    connect(ui->drawLeaves, SIGNAL(stateChanged(int)), this, SLOT(drawLeaves(int)));
    connect(ui->BHbutton, SIGNAL(clicked(bool)), this, SLOT(BHstateChanged(bool)));
    connect(ui->PriorityButton, SIGNAL(clicked(bool)), this, SLOT(PriorityChanged(bool)));
    connect(ui->ApicalDepth, SIGNAL(valueChanged(int)),this, SLOT(apicalControl(int)));
    connect(ui->IterateAll, SIGNAL(clicked()), this,SLOT(iterateAll()));
    connect(ui->saveSceneButton, SIGNAL(clicked()), this, SLOT(saveScene()));
    connect(ui->loadSceneButton, SIGNAL(clicked()), this, SLOT(loadScene()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::iterateAll(){
    if(ui->tabWidget->currentIndex() == 0)
        ui->widget->m_scene->iterateAll();
    else if(ui->tabWidget->currentIndex() ==1 )
        ui->widget_2->iterateTree();
}

void MainWindow::BHstateChanged(bool b){
    ui->widget->m_scene->useBH(b);
}

void MainWindow::PriorityChanged(bool b){
    ui->widget->m_scene->useBH(!b);
}

void MainWindow::apicalControl(int b){
        ui->widget->m_scene->appicalControl(b);
}

void MainWindow::addMultipleMetamers(int b){
    if(b==0){
        ui->widget->m_scene->addMultipleMetamers(false);
        ui->widget_2->scene->addMultipleMetamers(false);
    }
    else{
        ui->widget->m_scene->addMultipleMetamers(true);
        ui->widget_2->scene->addMultipleMetamers(true);
    }
}

void MainWindow::drawLeaves(int b){
    if(b == 0){
        ui->widget->m_scene->drawer->m_drawLeaves = false;
    }
    else{
        ui->widget->m_scene->drawer->m_drawLeaves = true;

    }
}

void MainWindow::drawAlgo(int b){
    ui->widget_2->scene->drawAlgo = b;
    ui->widget->m_scene->drawAlgo = b;
}

void MainWindow::shadowSelected(bool b){
    ui->widget_2->scene->shadow = true;
    ui->widget->m_scene->shadow = true;
    ui->shadowProp->setChecked(true);
    ui->spaceCol->setChecked(false);

}

void MainWindow::shadowDeselected(bool b){
    ui->shadowProp->setChecked(false);
    ui->spaceCol->setChecked(true);
    ui->widget_2->scene->shadow = false;
    ui->widget->m_scene->shadow = false;


}

void MainWindow::set_text_lambda(int val){
    float scaled_val = ((float) val)/100.0;
    ui->widget->lambda = scaled_val;
    ui->widget_2->lambda = scaled_val;
    ui->label_8->setText(QString::number(scaled_val,'g', 3));
}

void MainWindow::set_text_tropism(int val){
        float scaled_val = 3*(((float) val)/100.0 - 0.5);
        ui->widget->tropism = scaled_val;
        ui->widget_2->tropism = scaled_val;
    ui->label_9->setText(QString::number(scaled_val,'g', 3));
}
void MainWindow::set_text_aux_angle(int val){
            float scaled_val = (float) val/100.0 * 3.14;
            ui->widget->aux_angle = scaled_val;
            ui->widget_2->aux_angle = scaled_val;
    ui->label_10->setText(QString::number(scaled_val,'g', 3));
}
void MainWindow::set_text_shed_thres(int val){
            float scaled_val = (float) val / 100.0+ 0.25;
            ui->widget->shed_thres = scaled_val;
            ui->widget_2->shed_thres = scaled_val;
    ui->label_11->setText(QString::number(scaled_val,'g', 3));
}
void MainWindow::set_text_term_weight(int val){
            float scaled_val = (float) val /5.0;
            ui->widget->term_weight = scaled_val;
            ui->widget_2->term_weight = scaled_val;
    ui->label_12->setText(QString::number(scaled_val,'g', 3));
}
void MainWindow::set_text_aux_weight(int val){
            float scaled_val = (float) val /5.0;
            ui->widget->aux_weight = scaled_val;
            ui->widget_2->aux_weight = scaled_val;
    ui->label_13->setText(QString::number(scaled_val,'g', 3));
}

void MainWindow::on_pushButton_clicked()
{
    //clear
    ui->widget_2->clearTree();
    ui->widget->clear();

}

void MainWindow::on_pushButton_2_clicked()
{
    ui->widget->applyValues();
    ui->widget_2->applyValues();
}

void MainWindow::on_pushButton_3_clicked()
{
    //iterate
    //widget 2 is 2D,
    //widget is 3D
        if(ui->tabWidget->currentIndex() == 0)
            ui->widget->iterate();
        else if(ui->tabWidget->currentIndex() ==1 )
            ui->widget_2->iterateTree();
}

void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    //VALUES RANGE FROM 0 to 99
    ui->widget->lambda = value;
    ui->widget_2->lambda = value;
}

void MainWindow::on_horizontalSlider_2_valueChanged(int value)
{
    ui->widget->tropism = value;
    ui->widget_2->tropism = value;
}

void MainWindow::on_horizontalSlider_3_valueChanged(int value)
{
    ui->widget->aux_angle = value;
    ui->widget_2->aux_angle = value;
}

void MainWindow::on_horizontalSlider_4_valueChanged(int value)
{
    ui->widget->shed_thres = value;
    ui->widget_2->shed_thres = value;
}

void MainWindow::on_horizontalSlider_5_valueChanged(int value)
{
    ui->widget->term_weight = value;
    ui->widget_2->term_weight = value;
}

void MainWindow::on_horizontalSlider_6_valueChanged(int value)
{
    ui->widget->aux_weight = value;
    ui->widget_2->aux_weight = value;

}


void MainWindow::on_actionView_triggered(bool checked)
{
    ui->widget_2->scene->toggleInteract( !checked);

}

void MainWindow::on_actionDraw_triggered(bool checked)
{
    ui->widget_2->scene->toggleInteract( checked);
}

void MainWindow::on_actionView_triggered(){
    ui->actionView->setChecked(true);
    ui->actionDraw->setChecked(false);
    ui->widget->modeInteract = false;
    ui->widget_2->modeInteract = false;
}
void MainWindow::on_actionDraw_triggered(){
    ui->actionView->setChecked(false);
    ui->actionDraw->setChecked(true);
    ui->widget->modeInteract = true;
    ui->widget_2->modeInteract = true;
}

void MainWindow::on_actionQuit_triggered()
{
    exit(0);
}

void MainWindow::on_checkBox_toggled(bool checked)
{
    if (checked){
        ui->widget->grass_on = true;
    }
    else{
        ui->widget->grass_on = false;
    }
}

void MainWindow::on_checkBox_2_toggled(bool checked)
{
    if (checked){
        ui->widget->bark_mode = true;
    }
    else{
        ui->widget->bark_mode = false;
    }
}

void MainWindow::on_pushButton_4_clicked()
{
    ui->widget->updateBark();
}


void MainWindow::saveScene()
{
    cout << "save scene button clicked" << endl;
    cout << "filename:" << ui->fileNameTextBox->text().toStdString() << endl;

    if(ui->tabWidget->currentIndex() == 0)
    {
        if  (!ui->widget->saveScene(ui->fileNameTextBox->text()))
            QMessageBox::warning(this, tr("Self Organizing Trees 3D:"),
                                            tr("Error: Cannot open file. Input again."),
                                            QMessageBox::Ok,
                                            QMessageBox::Ok);
        else
            QMessageBox::warning(this, tr("Self Organizing Trees 3D:"),
                                            tr("Scene saved successfully."),
                                            QMessageBox::Ok,
                                            QMessageBox::Ok);

    }
    else if(ui->tabWidget->currentIndex() ==1 )
    {
        if  (!ui->widget_2->saveScene(ui->fileNameTextBox->text()))
            QMessageBox::warning(this, tr("Self Organizing Trees 2D:"),
                                            tr("Error: Cannot open file. Input again."),
                                            QMessageBox::Ok,
                                            QMessageBox::Ok);
        else
            QMessageBox::warning(this, tr("Self Organizing Trees 2D:"),
                                            tr("Scene saved successfully."),
                                            QMessageBox::Ok,
                                            QMessageBox::Ok);
    }
}

void MainWindow::loadScene()
{
    cout << "load scene button clicked" << endl;
    cout << "filename:" << ui->fileNameTextBox->text().toStdString() << endl;
    if(ui->tabWidget->currentIndex() == 0)
    {
        if  (!ui->widget->loadScene(ui->fileNameTextBox->text()))
            QMessageBox::warning(this, tr("Self Organizing Trees 3D:"),
                                            tr("Error: File not found or wrong format. Input again."),
                                            QMessageBox::Ok,
                                            QMessageBox::Ok);
        else
            QMessageBox::warning(this, tr("Self Organizing Trees 3D:"),
                                            tr("Scene loaded successfully."),
                                            QMessageBox::Ok,
                                            QMessageBox::Ok);
    }
    else if(ui->tabWidget->currentIndex() ==1 )
    {
        if  (!ui->widget_2->loadScene(ui->fileNameTextBox->text()))
            QMessageBox::warning(this, tr("Self Organizing Trees 2D:"),
                                            tr("Error: File not found or wrong format. Input again."),
                                            QMessageBox::Ok,
                                            QMessageBox::Ok);
        else
            QMessageBox::warning(this, tr("Self Organizing Trees 2D:"),
                                            tr("Scene loaded successfully."),
                                            QMessageBox::Ok,
                                            QMessageBox::Ok);
    }
}

